<?php
class Word_model extends CI_Model
{
	
	public function __construct()
	{
		$this->load->database();
	}
////////////////////////////////////////////////////////
//******************下面是word表的操作***************//
	public function getWords($start = 0,$id = '',$source='word')
	{
		$start = rand(0,$this->getMax($id,$source)-$this->getLearned($id));
		$num = (int)$start < 0 ? 0 : (int)$start;
		
		//$num = 10*(int)$num > (int)$this->getCount($id) ? 0 : (int)$num;
		
		$sql = 'SELECT word,word_id as auto_id,record.fre as fre from record,'.$source.' where record.word_id= '.$source.'.auto_id and record.owner_id = ? and record.table_name = ? and fre = 0 ORDER by record.fre ASC,record.word_id ASC limit '.$start.',50;';
		$result = $this->db->query($sql, array($id,$source))->result_array();
		return count($result) > 0 ? $result : array(); 
	}

	public function getRecords($start = 0,$id = '',$source='word')//返回前三条的记录
	{
		$start = rand(0,$this->getLearned($id));
		$num = (int)$start < 0 ? 0 : (int)$start;
		
		//$num = 10*(int)$num > (int)$this->getCount($id) ? 0 : (int)$num;
		
		$sql = 'SELECT word,word_id as auto_id,record.fre as fre from record,'.$source.' where record.word_id= '.$source.'.auto_id and record.owner_id = ? and record.table_name = ? and record.fre > 0 ORDER by record.fre ASC,record.word_id ASC limit '.$start.',20;';
		$result = $this->db->query($sql, array($id,$source))->result_array();
		return count($result) > 0 ? $result : array();
	}
	
    
	///////////////////
	public function getMean($id,$own,$source='word')
	{
		if($id > $this->getMax($own,$source))
		{
			return 'NULL';
		}
		$sql = 'SELECT meaning FROM '.$source.' WHERE auto_id = ? ;';
		return $this->db->query($sql,array($id))->row_array()['meaning'];
	}
	public function getRead($id,$own,$source='word')
	{
		if($id > $this->getMax($own,$source))
		{
			return 'NULL';
		}
		$sql = 'SELECT reading FROM '.$source.' WHERE auto_id = ? ;';
		return $this->db->query($sql,array($id))->row_array()['reading'];
	}
	
////////////////////////////////////////////////////////
//******************下面是record表的操作***************//
	public function addFre($id,$own,$source='word')
	{
		if($id > $this->getMax($own,$source))
		{
			return 'NULL';
		}
		$sql = 'UPDATE '.CI_Model::$record.' SET fre = fre+1,update_time = ? WHERE word_id = ? and owner_id =? and table_name = ?;';
		$this->db->query($sql,array(date('Y-m-d H:i:s'),$id,$own,$source));//在这里和record联动
		return 'TRUE';
	}
	public function getCount($id='',$source = 'word')
	{
		$sql = 'SELECT count(auto_id) as count FROM '.CI_Model::$record.' where owner_id = ? and table_name = ?;';
		return $this->db->query($sql,array($id,$source))->row_array()['count']; 
	}
	public function getCot($id='',$source = 'word')
	{
		$sql = 'select count(auto_id) as c from record where record.owner_id = ? and record.table_name = ?;';
		return $this->db->query($sql,array($id,$source))->row_array();
	}
	public function getMax($id ='',$source = 'word')
	{
		$sql = 'SELECT MAX(auto_id) as max FROM '.$source.' where 1 ;';
		return $this->db->query($sql,array($id))->row_array()['max']; 
	}
	
    public function getCountToday($user_id = '')
    {
    	$sql ='SELECT COUNT(auto_id) as count from '.CI_Model::$record.' where DATE_FORMAT(update_time,"%Y-%m-%d") = CURRENT_DATE and owner_id = ?;';
    	$result = $this->db->query($sql,array($user_id))->row_array();
    	return isset($result['count']) ? (int)$result['count'] : 0;
    }
    protected function getLearned($user_id = '')
    {
    	$sql ='SELECT count(auto_id) as count FROM '.CI_Model::$record.' WHERE owner_id = ? and fre > 0';
    	$result = $this->db->query($sql,array($user_id))->row_array();
    	return isset($result['count']) ? (int)$result['count'] : 0;
    }
	public function getTableInfo($user_id)
	{
		$sql = 'select DISTINCT(table_name) as name,count(auto_id) as count from record where record.owner_id = ? and fre > 0 ;';
		$result = $this->db->query($sql,array($user_id))->result_array();
		return  $result ;
	}
	public function addWordRecord($user_id='',$source = 'word')
	{
		if($this->getCount($user_id,$source) > 0)
		{
			return 0;
		}
		$sql = 'SELECT auto_id as word_id FROM '.$source.' WHERE 1;';
		$result = $this->db->query($sql)->result_array();
		$num = array();
		$query = "insert into record (word_id,owner_id) values ";
		for($i = 0;$i<count($result)-1;$i++)
		{
			$query=$query.'('.$result[$i]['word_id'].','.$user_id.'),';
		}
		$query=$query.'('.$result[count($result)-1]['word_id'].','.$user_id.')';
		$update = 'UPDATE record SET table_name = ?, fre = 0 ,create_time = ? where owner_id = ? and table_name = "mode";';
		$this->db->query($query);
		return $this->db->query($update,array($source,date("Y-m-d H:i:s"),$user_id));
	}
}